// 引入需要的模块
var express = require('express');
var router = express.Router();
var	wechate = require('wechat-enterprise');

//数据库操作对象
var DbOpt = require("../models/Dbopt");
var WeixinShow= require("../models/WeixinShow");
var WeixinOrder= require("../models/WeixinOrder");
var WeixinUser= require("../models/WeixinUser");

//配置文件
var settings = require("../models/db/settings");
var enterpriseConfg=settings.enterpriseConfg;

// 主页路由
router.get('/init', function(req, res) {
	var week = req.query.week;
	var queryCondition={};
	if(week==null||week==""){
		week=new Date().getDay();
		week=week==0?week=7:week;
	}
	var qc={};
	qc["wxid"]=req.query.wxid;
	qc["wxid"]=qc["wxid"]==null||qc["wxid"]==""?"123":qc["wxid"]
	queryCondition["week"]=week+"";
	DbOpt.pagination(WeixinShow,queryCondition,1,999,function(qinfo1){
		DbOpt.pagination(WeixinUser,qc,1,999,function(qinfo2){
			var users=qinfo2.docs;
			var weixinshows=qinfo1.docs;
			var phone="",address="",name="";
			if(users!=null&&users.length>0){
				name=users[0].name;
				phone=users[0].phone;
				address=users[0].address;
			}
			var ra=[];
			var ca=[];
			for(var i=0;i<weixinshows.length;i++){
				if(weixinshows[i].cstype==1)
					ra.push(weixinshows[i]);
				else
					ca.push(weixinshows[i]);
			}

			res.render("order/order", {
				ca : ca,
				ra : ra,
				moment : require("moment"),
				name:name,
				phone:phone,
				address:address,
				wxid:req.query.wxid,
				layout: null,

			});
		});
	});

});

//下订单
router.post('/confirm', function(req, res) {
	var addData=req.body.postData;
	DbOpt.addOne(WeixinOrder,addData,function(){
		var adduserdata={},condition={};
		adduserdata["wxid"]=addData["wxid"];
		adduserdata["name"]=addData["name"];
		adduserdata["phone"]=addData["phone"];
		adduserdata["address"]=addData["address"];
		adduserdata["remark"]=addData["remark"];

		condition["wxid"]=addData["wxid"];
		if(addData["wxid"]!=null&&addData["wxid"]!=="") {
			DbOpt.update(WeixinUser,condition,addData,function(){
				var reMsg = "";
				reMsg = "您已确认订单成功！";
				var bMsg = "您有新的订单请注意处理\n";
				var orderData = addData.items;
				for (var i = 0; i < orderData.length; i++) {
					bMsg += (i + 1) + "." + orderData[i].name + " x" + orderData[i].count + "份\n";
				}
				bMsg += "\n送货信息:\n"
				bMsg += "名称:" + addData["name"] + "\n";
				bMsg += "号码:" + addData["phone"] + "\n";
				bMsg += "地址:" + addData["address"] + "\n";
				bMsg += "留言内容:" +  addData["remark"]  + "\n";
				bMsg += " > " + new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString();
				msgToAll(bMsg);
				res.json({success: 1});
			});
		}else{
			var reMsg = "";
			reMsg = "您已确认订单成功！";
			var bMsg = "您有新的订单请注意处理\n";
			var orderData = addData.items;
			for (var i = 0; i < orderData.length; i++) {
				bMsg += (i + 1) + "." + orderData[i].name + " x" + orderData[i].count + "份\n";
			}
			bMsg += "\n送货信息:\n"
			bMsg += "名称:" + addData["name"] + "\n";
			bMsg += "号码:" + addData["phone"] + "\n";
			bMsg += "地址:" + addData["address"] + "\n";
			bMsg += "留言内容:" + addData["remark"] + "\n";
			bMsg += " > " + new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString();
			msgToAll(bMsg);
			res.json({success: 1});
		}
	});
});

var msgToAll=function(msg){
	var to={'touser':'@all'};
	var message={ "msgtype": "text","text": {
	       "content": msg
	   }};
	var api = new wechate.API(enterpriseConfg.corpId, enterpriseConfg.secret, enterpriseConfg.agentid);
	api.send(to,message, function (err, data, rest) {
		console.log("已发送全体信息!");
	});
}

module.exports = router;